package com.ipzoe.web.core.config;

import com.ipzoe.common.captcha.CaptchaCodeGenerator;
import com.ipzoe.common.captcha.CaptchaCodeManager;
import com.ipzoe.common.captcha.CaptchaCodeRepository;
import com.ipzoe.common.captcha.DefaultCaptchaCodeGenerator;
import com.ipzoe.common.captcha.config.CaptchaCodeProperties;
import com.ipzoe.common.captcha.repository.DefaultCaptchaCodeRepository;
import com.ipzoe.common.captcha.repository.RedisCaptchaCodeRepository;
import com.ipzoe.common.jwt.JwtTokenUtil;
import com.ipzoe.common.sms.AliSmsManager;
import com.ipzoe.common.sms.SmsManager;
import com.ipzoe.common.sms.config.AliSmsProperties;
import com.ipzoe.framework.config.RedisConfig;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.xml.bind.annotation.XmlType;

@Configuration
@AutoConfigureAfter(RedisConfig.class)
public class AppConfig {

    @Bean
    public JwtTokenUtil jwtTokenUtil(){
        return new JwtTokenUtil("u47REBDLrxfd88w",828000L);
    }

    @Bean
    @ConfigurationProperties(prefix = "sms.ali")
    public AliSmsProperties aliSmsProperties() {
        return new AliSmsProperties();
    }

    @Bean
    public SmsManager aliSmsManager(AliSmsProperties aliSmsProperties) {
        return new AliSmsManager(aliSmsProperties);
    }


    @Bean
    @ConfigurationProperties(prefix = "captcha.code")
    public CaptchaCodeProperties captchaCodeProperties() {
        return new CaptchaCodeProperties();
    }


    @Bean
    public CaptchaCodeManager captchaCodeManager(CaptchaCodeRepository captchaCodeRepository,
                                                 CaptchaCodeGenerator captchaCodeGenerator) {
        CaptchaCodeManager codeManager = new CaptchaCodeManager();
        codeManager.setCaptchaCodeGenerator(captchaCodeGenerator);
        codeManager.setCaptchaCodeRepository(captchaCodeRepository);
        return codeManager;
    }

    /**
     * 验证码内存管理器
     */
//    @Bean
//    @ConditionalOnMissingBean(name = {"redisTemplate", "captchaCodeRepository"})
//    public CaptchaCodeRepository captchaCodeRepository() {
//        return new DefaultCaptchaCodeRepository();
//    }

    @Bean
    public CaptchaCodeRepository captchaCodeRepository(RedisTemplate<String, Object> redisTemplate, CaptchaCodeProperties captchaCodeProperties) {
        RedisCaptchaCodeRepository captchaCodeRepository = new RedisCaptchaCodeRepository();
        captchaCodeRepository.setRedisTemplate(redisTemplate);
        captchaCodeRepository.setCaptchaCodeProperties(captchaCodeProperties);
        return captchaCodeRepository;
    }

    @Bean
    public CaptchaCodeGenerator captchaCodeGenerator(CaptchaCodeProperties captchaCodeProperties) {
        DefaultCaptchaCodeGenerator codeGenerator = new DefaultCaptchaCodeGenerator();
        codeGenerator.setCodeProperties(captchaCodeProperties);
        return codeGenerator;
    }

}
